package com.jxt.myjxtmanager.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.jxt.myjxtmanager.BaseFragment;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.adapter.GalleryAdapter;
import com.jxt.myjxtmanager.entity.AskForLeaveBean;
import com.jxt.myjxtmanager.ui.inter.AskLeaveInter;
import com.jxt.myjxtmanager.ui.presenter.AskLeavePresenter;
import com.jxt.myjxtmanager.utils.ListUtils;
import com.jxt.myjxtmanager.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/3/10 0010.
 */
public class LeaveForAnswerFragment extends BaseFragment implements AskLeaveInter, GalleryAdapter.OnItemClackLisenter {
    private RecyclerView mRecyclerView;
    private List<AskForLeaveBean> mDatas;
    private GalleryAdapter mAdapter;
    private AskLeavePresenter presenter;
    private BarChart mBarChart;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_leave_for_answer;
    }

    @Override
    protected void initView() {
        //得到控件
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.id_recyclerview_horizontal);
        mBarChart = (BarChart) mView.findViewById(R.id.mBarChart);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        presenter = new AskLeavePresenter(getActivity(), this);
        presenter.RequestStudentData();

//        mBarChart.getDescription().setEnabled(false);
//        mBarChart.setMaxVisibleValueCount(55);
//        mBarChart.setPinchZoom(false);
//        mBarChart.setDrawBarShadow(false);
//        mBarChart.setDrawGridBackground(false);
//        XAxis xAxis = mBarChart.getXAxis();
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//        xAxis.setDrawGridLines(false);
//        mBarChart.getAxisLeft().setDrawGridLines(false);
//        mBarChart.animateY(2500);
//        mBarChart.getLegend().setEnabled(false);
//        setData();
    }

    @Override
    public void setMyAdapter() {
        presenter.dismissDialog();
        mDatas = presenter.getDataList();
        if (ListUtils.isEmpty(mDatas)) return;
        //设置适配器
        mAdapter = new GalleryAdapter(getActivity(), mDatas, this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void updateRecycler() {
        presenter.dismissDialog();
        mAdapter.notifyDataSetChanged();
        if (mDatas != null && mDatas.size() <= 0) {
            mRecyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    public void showToastMsg(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            ToastUtil.showToast(getActivity(), msg);
        }
    }

    @Override
    public void onCancle(int position) {
        mDatas.remove(position);
        presenter.RefreshDatas();
    }

    @Override
    public void onSure(int position) {
        mDatas.remove(position);
        presenter.RefreshDatas();
    }

    //设置数据
    private void setData() {
        //设置相关属性
//        mBarChart.setOnChartValueSelectedListener(this);
        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawValueAboveBar(true);
        mBarChart.getDescription().setEnabled(false);
        mBarChart.setMaxVisibleValueCount(60);//一屏超过25列时不显示具体数值，设置超过60无效
        mBarChart.setPinchZoom(false);
        mBarChart.setDrawGridBackground(false);
        mBarChart.getAxisRight().setEnabled(false); // 隐藏右边的y轴
        mBarChart.getXAxis().setDrawGridLines(false); // 隐藏网格竖线
        //x轴
        XAxis xl = mBarChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        xl.setDrawAxisLine(true);
        xl.setDrawGridLines(false);
        xl.setGranularity(1f);

        //y轴
        YAxis yl = mBarChart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(true);
        yl.setAxisMinimum(0f);
        yl.setAxisMaxValue(55);    //设置Y轴坐标最大为多少
//        //y轴
//        YAxis yr = mBarChart.getAxisRight();
//        yr.setDrawAxisLine(true);
//        yr.setDrawGridLines(false);
//        yr.setAxisMinimum(0f);
//        yr.setAxisMaxValue(55);

        //设置数据
        setData(5);
        mBarChart.setFitBars(true);
        mBarChart.animateY(2500);

        Legend l = mBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);
    }

    private float[] floats = new float[]{4f, 2f, 0f, 1f, 5f};

    private void setData(int count) {
        float barWidth = 0.9f;
        float spaceForBar = 10f;
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < count; i++) {
//            float val = (float) (Math.random() * range);
            float val = floats[i];
            yVals1.add(new BarEntry((i+1), val));
        }
        BarDataSet set1;
        if (mBarChart.getData() != null &&
                mBarChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mBarChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mBarChart.getData().notifyDataChanged();
            mBarChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "迟到人数");

            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);

            data.setBarWidth(barWidth);
            mBarChart.setData(data);
        }
    }
}